package com.aco.party.web.controller.system;

import com.aco.party.model.po.Hire;
import com.aco.party.model.vo.HireParam;
import com.aco.party.model.vo.HireVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.HireService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditHireCommand;
import com.aco.party.web.validator.system.EditHireValidator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;
/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageHireController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageHireController.class);

    private static final String[] cityNames = new String[]{"北京","上海","广州"};

    @Resource
    private HireService hireService;

    @Resource
    private MetaService metaService;

    private EditHireValidator validator = new EditHireValidator();

    private final String REDIRECTEDIT = "redirect:edit";

    @RequestMapping(value = {"/hire", "/hire/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageHires";
    }

    @RequestMapping(value = "hire/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <HireVo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <HireVo> page = new Page<>();
        entry.webParamConvert();
        List <HireVo> list = hireService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),new HireParam());
        page.setTotal(hireService.getTotal(new HireParam()));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/hire/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditHireCommand command, Model model) {
        if (command.getId() != null) {
            Hire po = hireService.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        return "system/editHire";
    }

    @RequestMapping(value = "/hire/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditHireCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Hire hire = new Hire();
                command.toPo(hire);
            hire.setUpdator(getCurrentUser().getId());
            hire.setCity(getCityName(hire.getAddress()));
            hire.setUpdateTime(new Date());
            hireService.update(hire);
            model.addAttribute("no_error",true);
        }
        return "system/editHire";
    }

    @RequestMapping(value = "/hire/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditHireCommand command) {
        command.setStatus(1);
        return "system/editHire";
    }

    @RequestMapping(value = "/hire/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditHireCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editHire";
        }
        Hire hire = new Hire();
        command.toPo(hire);
        hire.setCreator(getCurrentUser().getId());
        hire.setCity(getCityName(hire.getAddress()));
        hire.setCreateTime(new Date());
        hireService.add(hire);
        attributes.addAttribute("id",hire.getId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/hire/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        hireService.delete(id);
        return new Response();
    }

    private void setAtt(Model model){

    }

    private String getCityName(String address){
        if(StringUtils.isNotEmpty(address)){
            for(String city : cityNames) {
                if(address.contains(city)){
                    return city;
                }
            }
        }
        return "";
    }


}
